package org.bluweb.g4wicket.language;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.bluweb.g4wicket.G4WicketBehaviour;

@SuppressWarnings("serial")
public class LanguageBranding extends G4WicketBehaviour {

	public static enum Format { VERTICAL, HORIZONTAL };
	
	private final List<Component> components = new ArrayList<Component>();
	
	private Format format = Format.HORIZONTAL;
	
	@Override
	public void bind(Component component) {
		this.components.add( component );
	}

	public Format getFormat() {
		return format;
	}

	public void setFormat(Format format) {
		this.format = format;
	}

	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead(response);
		
		StringBuilder builder = new StringBuilder( "google.load(\"language\",\"1\");" );

		builder.append( "function languageBrand() {" );
		builder.append( "var v = {type:'" );
		builder.append( this.format.toString().toLowerCase() );
		builder.append( "'};" );
		for( Component component : this.components ) {
			builder.append( "google.language.getBranding('" );
			builder.append( component.getMarkupId() );
			builder.append( "',v);" );
		}
		
		builder.append( "} google.setOnLoadCallback(languageBrand);" );
		
		response.renderJavascript( builder.toString(), "language-branding" );
	}
}
